package com.blyat.xsoft.gateway.autoconfigurator;

import com.blyat.xsoft.gateway.ribbon.router.AuthenticateRouteService;
import com.blyat.xsoft.gateway.security.service.SecurityUserService;
import com.blyat.xsoft.gateway.security.core.XSoftReactiveAuthorizationManager;
import com.blyat.xsoft.kernel.component.security.oauth2.token.jwt.OauthJwtAccessTokenConverter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.oauth2.provider.token.AccessTokenConverter;
import org.springframework.security.oauth2.provider.token.TokenStore;
import org.springframework.security.oauth2.provider.token.store.JwtAccessTokenConverter;
import org.springframework.security.oauth2.provider.token.store.JwtTokenStore;

/**
 * @Auther: syh
 * @Date: 2020/7/15
 * @Description:
 */
@Configuration
public class TokenConfiguration {

    @Autowired
    private AuthenticateRouteService routeService;

    @Bean
    public TokenStore tokenStore(AccessTokenConverter converter) {
        return new JwtTokenStore((JwtAccessTokenConverter) converter);
    }

    @Bean
    public AccessTokenConverter accessTokenConverter() {
        JwtAccessTokenConverter accessTokenConverter = new OauthJwtAccessTokenConverter(securityUserService());
        accessTokenConverter.setSigningKey("sign_key");
        return accessTokenConverter;
    }

    @Bean
    public XSoftReactiveAuthorizationManager xSoftReactiveAuthorizationManager() {
        XSoftReactiveAuthorizationManager manager = new XSoftReactiveAuthorizationManager();
        manager.setAuthenticateRouteService(routeService);
        return manager;
    }

    @Bean
    public UserDetailsService securityUserService() {
        return new SecurityUserService();
    }
}
